#!/usr/bin/env Python
# coding=utf-8
from sqlalchemy import Column
from sqlalchemy.types import CHAR, Integer, String
from sqlalchemy import func, or_, not_

import methods.database.dbORM as dbORM

def init_db():
    dbORM.BaseModel.metadata.create_all(dbORM.engine)

def drop_db():
    dbORM.BaseModel.metadata.drop_all(dbORM.engine)

def getUser(data):
    param=""
    for i in data:
        param=param+","+i+"='"+data[i]+"'"
    query=dbORM.session.query(Active_user).filter(param[1:]).scalar()
    print(query)
    return query

def addUser(data):
    query = dbORM.session.query(Active_user).filter("username='"+data['username']+"'").scalar()
    active_user = Active_user()
    if query==None:
        for i in data:
            setattr(active_user,i,data[i])
        dbORM.session.add(active_user)
        dbORM.session.commit()
    else:
        query.last_login_time = data['last_login_time']
        query.active_flag=data['active_flag']
        dbORM.session.commit()


class Active_user(dbORM.BaseModel):
    __tablename__ = 'active_user'
    id = Column(Integer, primary_key=True)
    username = Column(CHAR(50))
    last_login_time = Column(CHAR(50))
    active_flag = Column(CHAR(50))

    def __repr__(self):
        return "<Active_User(username='%s',  last_login_time='%s',  active_flag='%s')>" % (self.username, self.last_login_time, self.active_flag)
